package it.fuesi.synotools.adapters;

import android.app.Fragment;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import it.fuesi.synotools.R;
import it.fuesi.synotools.helpers.FormatHelper;
import it.fuesi.synotools.helpers.LayoutHelper;
import it.fuesi.synotools.model.btsearch.BTSearchClientParameters;
import it.fuesi.synotools.model.btsearch.BTSearchResultItem;
import it.fuesi.synotools.model.btsearch.BTSearchResultsDataObject;
import it.fuesi.synotools.services.tasks.search.BTSearchTask;

/**
 * Created by pascotto on 16/10/14.
 */
public class BTSearchAdapter extends SynoAdapter<BTSearchResultsDataObject>{

        protected static final String TAG = "DownloadTasksAdapter";

        protected List<BTSearchResultItem> data;
        private Context context;
        private Fragment fragment;
        private LayoutInflater inflater;



        @Override
        public Context getContext(){
            return this.context;
        }

        public BTSearchAdapter(LayoutInflater inflater, List<BTSearchResultItem> data, Context context, Fragment fragment)  {
            this.data = data;
            this.context=context;
            this.fragment=fragment;
            this.inflater = inflater;
        }





        @Override
        public void setItemCollection(BTSearchResultsDataObject data){
            this.data = data.getItems();
            notifyDataSetChanged();
        }

        /* (non-Javadoc)
         * @see android.widget.Adapter#getCount()
         */
        @Override
        public int getCount() {
            return data != null ? data.size() : 0;
        }

        /* (non-Javadoc)
         * @see android.widget.Adapter#getItem(int)
         */
        @Override
        public Object getItem(int index) {
            if(data != null && data.size() > index) {
                return data.get(index);
            }
            return null;
        }

        /* (non-Javadoc)
         * @see android.widget.Adapter#getItemId(int)
         */
        @Override
        public long getItemId(int id) {
            return id;
        }


        private class ViewHolder {
            TextView mResultName;
            TextView mResultSeeders;
            TextView mResultUri;
            TextView mResultSize;
        }

        private ViewHolder holder;

        /* (non-Javadoc)
         * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View vi=convertView;

            if(convertView==null){
                convertView = inflater.inflate(R.layout.search_result_item, null);

                holder = new ViewHolder();
                holder.mResultName = (TextView)convertView.findViewById(R.id.result_name);
                holder.mResultSeeders = (TextView)convertView.findViewById(R.id.result_seeders);
                holder.mResultUri = (TextView)convertView.findViewById(R.id.result_uri);
                holder.mResultSize = (TextView)convertView.findViewById(R.id.result_size);
                convertView.setTag(holder);

            }
            else {
                holder = (ViewHolder) convertView.getTag();
            }


            LayoutHelper.manageGridItemBackground(getContext(), convertView, position);




            final BTSearchResultItem item = data.get(position);


            holder.mResultName.setText(item.getTitle());
            holder.mResultSeeders.setText(""+item.getSeeds());
            holder.mResultUri.setText(item.getDownload_uri());
            holder.mResultSize.setText(FormatHelper.formatFileSize(item.getSize()));


            return convertView;


        }



        public void fetch(String keyword){
            BTSearchClientParameters params = BTSearchClientParameters.build(true);
            params.setKeyword(keyword);
            fetch(params);
        }

        public void fetch(BTSearchClientParameters params){
            new BTSearchTask(this).execute(params);
        }









}
